﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Utilities.QuickSql;
using MySql.Data;
using System.Threading;

namespace Deviseur_Cs2i_Car
{
    public partial class AddMotorisation : Form
    {
        private MOTORISATION Motorisation;

        public AddMotorisation()
        {
            InitializeComponent();

            this.Text = "Ajouter une motorisation";
            this.buttonAddMotorisation.Text = "Ajouter";
            /*******/
            DataTable DataMotor = new DataTable();

            DataMotor.Columns.Add("IDContenance");
            DataMotor.Columns.Add("Libelle");
            DataMotor.Rows.Add("0", "- Choisir une Contenance -");

            Table contenance = Program.Db.QueryTable("SELECT * FROM CONTENANCE");
            foreach (Record r in contenance)
                DataMotor.Rows.Add(r.Values[0], r.Values[1]);

            this.comboContenance.DataSource = DataMotor;
            this.comboContenance.ValueMember = "IDContenance";
            this.comboContenance.DisplayMember = "Libelle";


            this.comboContenance.SelectedIndex = 0;
            /*******/
            DataTable DataType = new DataTable();

            DataType.Columns.Add("IDType");
            DataType.Columns.Add("LibelleType");
            DataType.Columns.Add("NumHTDI");
            DataType.Rows.Add("0", "- Choisir un type -");

            Table type = Program.Db.QueryTable("SELECT * FROM TYPEMOTEUR");
            foreach (Record r in type)
                DataType.Rows.Add(r.Values[0], r.Values[1]);

            this.comboTypeMoteur.DataSource = DataType;
            this.comboTypeMoteur.ValueMember = "IDType";
            this.comboTypeMoteur.DisplayMember = "LibelleType";

            this.comboTypeMoteur.SelectedIndex = 0;
        }
        public AddMotorisation(int NumMotor)
        {
            InitializeComponent();
            this.Text = "Modifier une motorisation";
            this.buttonAddMotorisation.Text = "Modifier";

            DataTable DataMotor = new DataTable();

            DataMotor.Columns.Add("IDContenance");
            DataMotor.Columns.Add("Libelle");
            DataMotor.Rows.Add("0", "- Choisir une contenance -");

            Table contenance = Program.Db.QueryTable("SELECT * FROM CONTENANCE");
            foreach (Record r in contenance)
                DataMotor.Rows.Add(r.Values[0], r.Values[1]);

            this.comboContenance.DataSource = DataMotor;
            this.comboContenance.ValueMember = "IDContenance";
            this.comboContenance.DisplayMember = "Libelle";


            this.comboContenance.SelectedIndex = 0;
            /*******/
            DataTable DataType = new DataTable();

            DataType.Columns.Add("IDType");
            DataType.Columns.Add("LibelleType");
            DataType.Columns.Add("NumHTDI");
            DataType.Rows.Add("0", "- Choisir un type -");

            Table type = Program.Db.QueryTable("SELECT * FROM TYPEMOTEUR");
            foreach (Record r in type)
                DataType.Rows.Add(r.Values[0], r.Values[1]);

            this.comboTypeMoteur.DataSource = DataType;
            this.comboTypeMoteur.ValueMember = "IDType";
            this.comboTypeMoteur.DisplayMember = "LibelleType";

            this.comboTypeMoteur.SelectedIndex = 0; 
            /*******/
            
            Table toto = Program.Db.QueryTable("SELECT * FROM MOTORISATION WHERE NumMotor="+NumMotor);
            foreach (Record r in toto)
            {
                this.textLibelle.Text = r.Values[1].ToString();
                this.comboContenance.SelectedValue = r.Values[2].ToString();
                this.comboTypeMoteur.SelectedValue = r.Values[3].ToString();
                Motorisation = new MOTORISATION(NumMotor, this.textLibelle.Text.ToString(), Convert.ToInt32(this.comboContenance.SelectedValue), Convert.ToInt32(this.comboTypeMoteur.SelectedValue));
            }

            
        }
        private void addVoiture_Thread()
        {
            Invoke(new MethodInvoker(Disabled));
            Motorisation.new_Motorisation();
            Invoke(new MethodInvoker(CloseFen));
        }
        private void updateType_Thread()
        {
            Invoke(new MethodInvoker(Disabled));
            Motorisation.modify_Motorisation();
            Invoke(new MethodInvoker(CloseFen));
        }

        private void buttonAddMotorisation_Click_1(object sender, EventArgs e)
        {
            if (textLibelle.Text.ToString() != "" && comboTypeMoteur.SelectedValue.ToString() != "0" && comboContenance.SelectedValue.ToString() != "0")
            {
                if (this.buttonAddMotorisation.Text == "Ajouter")
                {
                    Motorisation = new MOTORISATION(this.textLibelle.Text.ToString(), Convert.ToInt32(this.comboTypeMoteur.SelectedValue.ToString()), Convert.ToInt32(this.comboContenance.SelectedValue.ToString()));

                    Thread addThread = new Thread(new ThreadStart(addVoiture_Thread));
                    addThread.Start();
                }
                else if (this.buttonAddMotorisation.Text == "Modifier")
                {
                    Motorisation.set_ChMotorisation(this.textLibelle.Text.ToString());

                    Motorisation.set_Contenance(new CONTENANCE(Convert.ToInt32(this.comboContenance.SelectedValue.ToString())));
                    Motorisation.set_TypeMoteur(new TYPEMOTEUR(Convert.ToInt32(this.comboTypeMoteur.SelectedValue.ToString())));
                    Thread updateThread = new Thread(new ThreadStart(updateType_Thread));
                    updateThread.Start();
                }
            }
            else
            {
                MessageBox.Show("Veuillez remplir tous les champs", "Erreur de saisie");
            }
        }
        private void Disabled()
        {
            this.textLibelle.Enabled = false;
            this.comboContenance.Enabled = false;
            this.comboTypeMoteur.Enabled = false;
            this.buttonAddMotorisation.Enabled = false;
        }
        private void CloseFen()
        {
            this.Close();
        }

    }
}
